//值班员：20485080740   领导：20485080744  网信办：20485080749
const userInfo = {
    id: 20485080744,
    roleRank: ""
};

const publicMethods = {
    backDetail(){
        store.dispatch("showFlag_action", 2);
    },
    togClass(el, className){
        if (el.classList) {
            el.classList.toggle(className);
        } else {
            let classes = el.className.split(' ');
            let existingIndex = classes.indexOf(className);

            if (existingIndex >= 0)
                classes.splice(existingIndex, 1);
            else
                classes.push(className);

            el.className = classes.join(' ');
        }
    }
};

const publicData = {
    peopleChecked: [],
    newsAddChecked: []
};

const ajax_ = {
    url: "/Cyberspace/sys/",
    timeOut: 3000,
    error(){
        mui.toast('服务器没有响应，请稍后重试！');
    },
    loading(val){
        store.commit("_loadingShow", val);
    },
    getTypeData(url, data, call){
        let self = this;
        mui.ajax(this.url + url, {
            data: data,
            dataType: 'json',
            type: 'get',
            timeout: this.timeOut,
            success(data){
                call(data);
            },
            error(){
                self.error();
            }
        });
    },
    getType(url, data, call){
        let self = this;
        mui.ajax(this.url + url, {
            data: data,
            dataType: 'json',
            type: 'get',
            timeout: this.timeOut,
            success(data){
                call(data.result);
            },
            error(){
                self.error();
            }
        });
    },
    postType(url, data, call){
        let self = this;
        mui.ajax(this.url + url, {
            data: data,
            dataType: 'json',
            type: 'post',
            timeout: this.timeOut,
            success(data){
                call(data);
            },
            error(){
                self.error();
            }
        });
    },
    deleteType(url, data, call){
        let self = this;
        mui.ajax(this.url + url, {
            data: data,
            dataType: 'json',
            type: 'delete',
            timeout: this.timeOut,
            success(data){
                call(data);
            },
            error(){
                self.error();
            }
        });
    },
    putType(url, data, call){
        let self = this;
        self.loading(true);
        mui.ajax(this.url + url, {
            data: data,
            dataType: 'json',
            type: 'put',
            timeout: this.timeOut,
            success(data){
                self.loading(false);
                call(data);
            },
            error(){
                self.loading(false);
                self.error();
            }
        });
    },
    confirmPutType(url, data, call){
        confirm_(() => {
            this.putType(url, data, call);
        });
    }
};

const alert_ = (call) => {
    mui.alert('处理成功', '', () => {
        call && call();
    });
};

const alertWithTxt = (str, call) => {
    mui.alert(str, '', () => {
        call && call();
    });
};

const alert_back = () => {
    alert_(() => {
        scrollTriggerDownScroll();
        goBack();
    })
};

const confirm_ = (call) => {
    mui.confirm('是否确认处理？', '', ['取消', '确认'], (e) => {
        if (e.index == 1) {
            call && call();
        }
    });
};

let vrv = vrv || {};

const vrvInit = {
    userInfo: "",
    letters: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
    contactList: [],
    contactListForSearch: [],
    getImgUrl(v){
        return v ? v : `./img/q.png`;
    },
    init(){
        vrv.ready(() => {
            vrv.jssdk.showNavigationBar({show:false});
            vrv.jssdk.getAccountInfo({
                success: function (res) {
                    userInfo.id = res.mUserId;
                    getUserType(() => {
                        APP.$emit("isPush", 1);
                    });
                }
            })
        });
    },
    getContacts(){
        //获取好友
        // vrv.jssdk.getContactList({
        //     success: function (res) {
        //         let peoples = {};
        //         let others = {letter: "#", arr: []};
        //         res.contacts.forEach((v) => {
        //             let l = Letter(v.buddyName).toString().charAt(0);
        //             v.imgUrl = vrvInit.getImgUrl(v.portraitURL);
        //             self.contactListForSearch.push(v);
        //             if (!self.letters.match(l)) {
        //                 others.arr.push(v);
        //             } else {
        //                 if (!peoples[l]) {
        //                     peoples[l] = {letter: '', arr: []};
        //                     peoples[l].letter = l;
        //                     peoples[l].arr.push(v);
        //                 } else {
        //                     peoples[l].arr.push(v);
        //                 }
        //             }
        //         });
        //         if (others.arr.length) {
        //             peoples.others = others;
        //         }
        //         if (!Object.keys(peoples).length) {
        //             peoples = false;
        //         } else {
        //             peoples = Object.keys(peoples).sort().map((i) => {
        //                 return peoples[i];
        //             });
        //         }
        //         self.contactList = peoples;
        //         // store.commit("_peopleList", peoples);
        //     }
        // })
    },
    closeWindow(){
        vrv.jssdk.closeView({});
    }
};

// type 1：今日快报  2：事件管理  3：预警管理
const getHeader = {
    type: "",
    txt: "",
    init(v){
        let str = "";
        switch (v) {
            case 1:
                str = `今日快报`;
                break;
            case 11:
                str = `添加选题`;
                break;
            case 12:
                str = `快报详情`;
                break;
            case 20:
                str = `${this.txt}管理`;
                break;
            case 2:
                str = `${this.txt}详情`;
                break;
            case 21:
                str = `通讯录`;
                break;
            case 111:
                str = `请示领导`;
                break;
        }
        return str;
    },
    getHeaderTxt(){
        let h = "";
        switch (parseInt(this.type)) {
            case 1:
                h = "今日快报";
                break;
            case 2:
                h = "事件";
                break;
            case 3:
                h = "预警";
                break;
        }
        return h;
    }
};

//事件基本信息
const eventKeys = {
    init: (v) => {
        return {
            title: "详情信息",
            data: [
                {
                    label: "标题",
                    type: 1,
                    value: v.title
                },
                {
                    label: "事件类型",
                    type: 1,
                    value: v.extend.firstEventType.name + " — " + v.extend.secondEventType.name
                },
                {
                    label: "事件等级",
                    type: 1,
                    value: eventKeys.eventLevelToString(v.extend.primaryEventGrade)
                },
                {
                    label: "发生日期",
                    type: 1,
                    value: v.happenTime
                },
                {
                    label: "报送单位",
                    type: 1,
                    value: v.createUserInfo.orgName
                },
                {
                    label: "填报人",
                    type: 1,
                    value: v.createUserInfo.linkdoodName
                },
                {
                    label: "联系电话",
                    type: 1,
                    value: v.createUserInfo.cellphone
                },
                {
                    label: "传真",
                    type: 1,
                    value: v.extend.fax
                },
                {
                    label: "事发单位及系统功能描述",
                    type: 2,
                    value: v.extend.unitSystemDescription
                },
                {
                    label: "事态发展概述及初判原因",
                    type: 2,
                    value: v.extend.primaryJudgment
                },
                // {
                //     label:"影响范围",
                //     type:2,
                //     value:v
                // },
                {
                    label: "影响地域",
                    type: 2,
                    value: v.extend.regionListString
                },
                {
                    label: "影响行业",
                    type: 2,
                    value: v.extend.industryListString
                },
                {
                    label: "影响和危害描述",
                    type: 2,
                    value: v.extend.influenceHarm
                },
                {
                    label: "已采取的措施及效果",
                    type: 2,
                    value: v.extend.measureToken
                },
                {
                    label: "请求事项及工作建议",
                    type: 2,
                    value: v.extend.itemsAdvise
                },
                {
                    label: "备注",
                    type: 2,
                    value: v.extend.remarks
                },
            ],
        }
    },
    eventLevelToString(v){
        return ['', '特别重大', '重大', '较大', '一般', '其他'][v];
    }
};

//预警基本信息
const warnKeys = {
    init: (v) => {
        return {
            title: "详情信息",
            data: [
                {
                    label: "标题",
                    type: 1,
                    value: v.title
                },
                {
                    label: "风险类型",
                    type: 1,
                    value: v.extend.riskType.name
                },
                {
                    label: "风险等级",
                    type: 1,
                    value: warnKeys.warnLevelToString(v.extend.primaryRiskGrade)
                },
                {
                    label: "预警等级",
                    type: 1,
                    value: warnKeys.riskTypeToString(v.extend.warningLevel)
                },
                {
                    label: "发生日期",
                    type: 1,
                    value: v.happenTime
                },
                {
                    label: "报送单位",
                    type: 1,
                    value: v.createUserInfo.orgName
                },
                {
                    label: "填报人",
                    type: 1,
                    value: v.createUserInfo.linkdoodName
                },
                {
                    label: "联系电话",
                    type: 1,
                    value: v.createUserInfo.cellphone
                },
                {
                    label: "传真",
                    type: 1,
                    value: v.extend.fax
                },
                {
                    label: "情况描述",
                    type: 2,
                    value: v.extend.basicSituationDescription
                },
                // {
                //     label:"影响范围",
                //     type:2,
                //     value:v
                // },
                {
                    label: "影响地域",
                    type: 2,
                    value: v.extend.regionListString
                },
                {
                    label: "影响行业",
                    type: 2,
                    value: v.extend.industryListString
                },
                {
                    label: "影响和危害描述",
                    type: 2,
                    value: v.extend.influenceHarm
                },
                {
                    label: "验证方法",
                    type: 2,
                    value: v.extend.verificationMethod
                },
                {
                    label: "趋势及危害",
                    type: 2,
                    value: v.extend.trendHazards
                },
                {
                    label: "已采取措施及效果",
                    type: 2,
                    value: v.extend.measureToken
                },
                {
                    label: "建议应对措施",
                    type: 2,
                    value: v.extend.suggestion
                },
                {
                    label: "备注",
                    type: 2,
                    value: v.extend.remarks
                },
            ],
        }
    },
    warnLevelToString(v){
        return ['', '很高', '高', '中等', '低', '很低'][v];
    },
    riskTypeToString(v){
        return ['无', '红', '橙', '黄', '蓝'][v];
    }
};

//请示意见列表点击记录格式化
const detailsFun = {
    approvalListLastID: "",
    operateTxt: ["请示领导", "同意", "退回"],
    //获取批示人ID
    approvalListFormat(val, status){
        let newArr = [];
        let btnFlag = false;
        val.forEach((i, index) => {
            if (i.approvalTime != null) {
                if (index === 0) {
                    i.operateTxt = "请示领导";
                } else {
                    i.operateTxt = this.operateTxt[parseInt(i[status]) - 1];
                }
                newArr.push(i);
            } else {
                this.approvalListLastID = i.id;
                if (i.userID == userInfo.id) {
                    btnFlag = true;
                }
            }
        });
        return {
            newArr,
            btnFlag,
        };
    },
}

//跳转选人页面
const goPeopleList = () => {
    goName("peopleList", 3);
};

//获取用户角色
const getUserType = (call) => {
    ajax_.getTypeData("user/" + userInfo.id, {}, (data) => {
        if (parseInt(data.code) === 0) {
            //用户不存在
            alertWithTxt(data.msg);
        } else {
            userInfo.roleRank = data.result.role.id;
            call();
        }
    });
};

//判断是否为推送
const isPush = {
    val: GetQueryString("isPush"),
    flag: false,
    init(call){
        if (this.val == 1) {
            //推送消息加载详情
            getHeader.type = GetQueryString("type");
            getHeader.txt = getHeader.getHeaderTxt();
            this.flag = true;
            browserRedirect();
            vrvInit.init();
        } else {
            //正常加载
            let v = JSON.parse(sessionStorage.getItem("urlVal"));
            //值班员：20485080740   领导：20485080744  网信办：20485080749
            userInfo.id = v.userInfo.createUserInfo.linkdoodID;
            getHeader.type = parseInt(sessionStorage.getItem("urlType"));
            // userInfo.id = 20485080740;
            // getHeader.type = 1;
            APP.$off("isPush");
            call();
        }
    }
};

function formatDate(date, fmt) {
    date = date == undefined ? new Date() : date;
    date = typeof date == 'number' ? new Date(date) : date;
    fmt = fmt || 'yyyy-MM-dd HH:mm:ss';
    var obj =
        {
            'y': date.getFullYear(), // 年份，
            'M': date.getMonth() + 1, // 月份，
            'd': date.getDate(), // 日期
            'q': Math.floor((date.getMonth() + 3) / 3), // 季度
            'w': date.getDay(), // 星期，
            'H': date.getHours(), // 24小时制
            'h': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 12小时制
            'm': date.getMinutes(), // 分钟
            's': date.getSeconds(), // 秒
            'S': date.getMilliseconds() // 毫秒
        };
    var week = ['日', '一', '二', '三', '四', '五', '六'];
    for (var i in obj) {
        fmt = fmt.replace(new RegExp(i + '+', 'g'), function (m) {
            var val = obj[i] + '';
            if (i == 'w') return (m.length > 2 ? '星期' : '周') + week[val];
            for (var j = 0, len = val.length; j < m.length - len; j++) val = '0' + val;
            return m.length == 1 ? val : val.substring(val.length - m.length);
        });
    }
    return fmt;
}

function GetQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    let params = "";
    let val = "";
    if (window.location.search) {
        params = window.location.search.substr(1).match(reg);
    } else if (window.location.hash && window.location.hash.split("?")[1]) {
        params = window.location.hash.split("?")[1].match(reg);
    }
    if (params != null) {
        val = decodeURI(params[2])
    }
    return val;
}

//判断是否为手机端
function browserRedirect() {
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
        //移动端
    }
    else {
        //PC端
        let type = GetQueryString("type");
        let id = GetQueryString("id");
        let url = "";
        switch (parseInt(type)) {
            case 1:
                url = `../../../Cyberspace/#/todayBulletinDesc?id=${id}`;
                break;
            case 2:
            case 3:
                url = `../../../Cyberspace/#/eventRiskDesc?id=${id}`;
                break;
        }
        window.location = url;
    }
}


